package com.tools.jstack;

import sun.tools.jstack.JStack;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.lang.management.ManagementFactory;

public class JStackDemo {
    /**
     * 打印当前进程的堆栈
     * @param args
     */
    public static void main(String[] args) throws FileNotFoundException {
        // get name representing the running Java virtual machine.
        String name = ManagementFactory.getRuntimeMXBean().getName();
        System.out.println(name);
        // get pid
        String pid = name.split("@")[0];
        System.out.println("Pid is:" + pid);
        System.out.println();
        String filePath = "./abc";
        PrintStream out = new PrintStream(new File(filePath));
        System.setOut(out);
        try {
            JStack.main(new String[]{pid});
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
